﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessMood : IBusinessMood
    {
        IDALMood _dalMood;
        IUnitOfWork _unitWork;
        public BusinessMood(IDALMood dalMood, IUnitOfWork unitWork)
        {
            _dalMood = dalMood;
            _unitWork = unitWork;
        }
        public void Add(MoodModel model)
        {
            var dataEntity = new Tbl_Mood { AccountId = model.AccountId, Content = model.Content, CreateTime = DateTime.Now, LastUpdateTime = DateTime.Now };
            _dalMood.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(MoodModel model)
        {
            var dataEntity = _dalMood.Get(model.ID);
            dataEntity.Content = model.Content;
            dataEntity.LastUpdateTime = DateTime.Now;
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<MoodModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Mood>>(models);
            _dalMood.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<MoodModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Mood>>(models);
            _dalMood.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<MoodModel> GetAll(bool isTracking = true)
        {
            var entities = _dalMood.GetAll(isTracking);
            return Mapper.Map<List<MoodModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalMood.Delete(primaryKey);
            _unitWork.Commit();
        }

        public MoodModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalMood.Get(primaryKey);
            return Mapper.Map<MoodModel>(dataEntity);
        }

        public void Delete(MoodModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Mood>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalMood.Delete(where);
            _unitWork.Commit();
        }

        public MoodModel Get(MoodModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Mood>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var dataEntity = _dalMood.Get(where);
            return Mapper.Map<MoodModel>(dataEntity);
        }

        public List<MoodModel> GetAll(MoodModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Mood>();
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var entities = _dalMood.GetAll(where, isTracking);
            return Mapper.Map<List<MoodModel>>(entities);
        }


        public List<MoodModel> GetAll(MoodModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void AddAgree(int id, string account)
        {
            var dataEntity = _dalMood.Get(id);
            dataEntity.AgreeUsers = string.IsNullOrWhiteSpace(dataEntity.AgreeUsers) ? account : dataEntity.AgreeUsers + "&" + account;
            _unitWork.Commit();
        }
    }
}
